package com.seo.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.proxy.sdk.ProxyUserService;
import com.seo.utils.DateUtil;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关controller
 * 
 * @author Administrator
 * @version 2018.08.02 17:40
 */
@Slf4j
@Controller
@RequestMapping(value = "/user/proxyUser")
public class ProxyUserManageController {

	@Autowired
	private ProxyUserService proxyUserService;

	@Autowired
	private PageTool pageTool;

	@RequestMapping
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpSession session) {
		log.info("进入代理客户列表...");
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		String yTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1);
		params.put("yTime", yTime);
		PageInfo<?> page = pageTool.getZdyPage(proxyUserService::getProxyUserCnt, proxyUserService::getProxyUserList,
				params);
		request.setAttribute("page", page);

		return "user/proxyUser/list";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		log.info("进入添加用户页面");
		request.setAttribute("action", "addUser");
		return "user/proxyUser/edit";
	}

	@RequestMapping("/addUser")
	public String addUser(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		rb = proxyUserService.addProxyUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/edit")
	public String edit(Integer userId, HttpServletRequest request) {
		log.info("进入编辑用户页面");
		Map<String, Object> proxyUser = proxyUserService.getProxyUserById(userId);
		request.setAttribute("proxyUser", proxyUser);
		request.setAttribute("action", "editUser");
		return "user/proxyUser/edit";
	}
	
	@RequestMapping("/editUser")
	public String editUser(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		rb = proxyUserService.updateProxyUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public ReturnBody deleteUser(Integer id, HttpSession session) {
		log.info("删除用户...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		return proxyUserService.deleteProxyUser(id);
	}
	
	@RequestMapping("/toResetPwd")
	public String toResetPwd(Integer userId, HttpServletRequest request) {
		log.info("进入用户重置密码页面");
		request.setAttribute("userId", userId);

		return "user/proxyUser/resetPwd";
	}
	
	@RequestMapping("/resetPwd")
	@ResponseBody
	public ReturnBody resetPwd(Integer id, String password, HttpSession session) {
		log.info("更改用户密码...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}
		
		
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		params.put("password", password);

		return proxyUserService.updateResetProxyUserPwd(params);
	}
	
	@RequestMapping("/updateState")
	@ResponseBody
	public ReturnBody updateState(Integer id,String state, HttpSession session) {
		log.info("更改用户状态...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		if(!(StringUtils.equals(state, "00") || StringUtils.equals(state, "01"))){
			return rb.error("error", "状态错误");
		}

		return proxyUserService.updateProxyUserState(id, state);
	}
}
